﻿using System;
using System.Reflection;

namespace StockholmsStad.Utilities
{
    public class Program
    {
        static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                var errorMessage = "Post-build event failed. Missing input parameter. Usage: SVCWriter <projDir> <targetPath> <configurationName>";
                WriteError(errorMessage);
                return 0;
            }

            var outputFileDir = args[0];
            var projAssembly = Assembly.LoadFrom(args[1]);
            var configuration = args[2];            

            foreach (var t in projAssembly.GetTypes())
            {
                if(IsPublicClass(t))
                {
                    SVCWriter.GenerateFile(outputFileDir, t, configuration);
                }
            }

            WebConfigWriter.GenerateFile(outputFileDir);

            return 0;
        }

        private static bool IsPublicClass(Type t)
        {
            return t.IsPublic && t.IsClass;
        }

        #region Errors and warnings

        // It's essential to keep the syntax exactly as specified below, otherwise 
        // it error messages won't be displayed during post build
        
        internal static void WriteWarning(string description)
        {
            WriteCompilerProblem("warning", Assembly.GetExecutingAssembly().GetName().Name, description);
        }

        internal static void WriteError(string description)
        {
            WriteCompilerProblem("error", Assembly.GetExecutingAssembly().GetName().Name, description);
        }

        internal static void WriteCompilerProblem(string type, string location, string description)
        {
            if (string.IsNullOrEmpty(location))
                location = "[N/A]";

            Console.WriteLine(location + " : " + type + " : " + ExeName + " : " + description);
        }

        internal static string ExeName
        {
            get { return Assembly.GetExecutingAssembly().GetName().Name; }
        }
        #endregion
    }
}
